package Logic

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"yz-go/Common"
	"yz-go/Models"
)

//type Ct struct {
//	id int
//	name int
//	parentid int
//	index int
//	leveltype int
//	pinyin int
//}

func GetCityList(page int, size int, c *gin.Context) (rd Common.ResData) {
	var ct []map[string]interface{}
	var hotct []map[string]interface{}
	var province []map[string]interface{}

	field := "id, `name`, parentid, `index`, leveltype, pinyin as url"
	Common.Err = Models.DB.Table("recruit_city").Where("`use` > ? and leveltype = ?", 0, 2).Select(field).Find(&ct).Error

	fmt.Printf("普通城市：%v", ct)

	Common.Err = Models.DB.Table("recruit_city").Where("`use` > ? and leveltype = ?", 0, 2).
		Select(field).
		Order("`use` desc").
		Limit(10).
		Find(&hotct).Error

	fmt.Printf("活跃中的城市：%v\n", hotct)

	// 获取ct中 所有城市id
	//var parentIds []interface{}
	//for _, item := range ct {
	//	if parentId, ok := item["parentid"].(int); ok {
	//		parentIds = append(parentIds, parentId)
	//	}
	//}

	var parentIds []interface{} // 可以直接放一个interface的切片
	for _, item := range ct {
		exists := false
		for _, id := range parentIds {
			if id == item["parentid"] {
				exists = true
			}
		}
		if !exists {
			parentIds = append(parentIds, item["parentid"])
		}
	}

	fmt.Printf("活跃中的城市ID：%v\n", parentIds)

	Models.DB.Table("recruit_city").Where("id in ?", parentIds).
		Select(field).
		Order("`use` desc").
		Limit(10).
		Find(&province)

	// 合并俩个城市和省份
	mergedMap := make([]map[string]interface{}, 0)
	// 将ct 中的每一个元素追加到 mergedMap
	mergedMap = append(mergedMap, ct...)
	mergedMap = append(mergedMap, province...)

	res := Common.MergedMapInterface(mergedMap)
	fmt.Printf("树形：%v\n", res)

	return Common.ResData{Common.SUCCESS, "", res}
	//
	//
	//
	//
	//query := Models.DB.Table("recruit_job as a")
	//
	//query = query.Where("a.is_deleted = ? ", Common.NOT_DELETE)
	//
	//workExp := c.Query("work_exp")
	//if workExp != "" {
	//	query = query.Where("a.work_exp = ? ", workExp)
	//}
	//field := "a.id,a.business_id,c.name as business_name,a.position_custom_name," +
	//	"a.position_category_child_id,b.name as position_category_child_name, a.work_exp," +
	//	"a.education,a.salary1,a.salary2,a.salary_month,a.welfare,a.city,a.area,a.email," +
	//	"a.release,a.release_job,a.job_type,a.release_mobile,a.describe,a.hot,a.show,a.created_at," +
	//	"a.status"
	//query = query.Select(field)
	//query = query.Joins("left join recruit_position_category as b ON a.position_category_child_id = b.id")
	//query = query.Joins("left join recruit_business as c ON a.business_id = c.id")
	//
	//// 获取总数
	//Common.Err = query.Count(&Common.TotalRows).Error
	//if Common.Err != nil {
	//	Common.ErrorResponse(c, Common.ERROR, Common.Err)
	//	return
	//}
	//if Common.TotalRows%int64(size) == 0 {
	//	Common.TotalPages = int(Common.TotalRows) / size
	//} else {
	//	Common.TotalPages = int(Common.TotalRows)/size + 1
	//}
	//
	//query = query.Limit(size).Offset((page - 1) * size).Order("a.created_at desc")
	//
	//Common.Err = query.Debug().Find(&citys).Error
	//
	//if Common.Err != nil {
	//	Common.ErrorResponse(c, Common.ERROR, Common.Err)
	//	return
	//}
	//
	//data := map[string]interface{}{"page": page, "size": size, "total": Common.TotalPages, "list": citys}
	//res := Common.ResData{Code: Common.SUCCESS, Msg: "", Data: data}
	//return res
}
